﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Abp.Mtrl
{
    public class MtrlHelper
    {

        public Abp.Mtrl.Material Get(int Id)
        {
            using (var db = new Abp.Mtrl.MtrlDbcontext())
            {
                return db.Material.Find(Id);
            }
        }

        public Abp.Mtrl.Material Update(Abp.Mtrl.Material sub)
        {
            using (var db = new Abp.Mtrl.MtrlDbcontext())
            {
                var mtrl= db.Material.Find(sub.Id);

                mtrl.CASNo = sub.CASNo;
                mtrl.CnName = sub.CnName;
                mtrl.MolecularFormula = sub.MolecularFormula;
                mtrl.MolecularWeight = sub.MolecularWeight;
                mtrl.MolString = sub.MolString;
                mtrl.EnName = sub.EnName;
                mtrl.RiskStatements = sub.RiskStatements;
                mtrl.ImgFile = sub.ImgFile;
                mtrl.CdxFile = sub.CdxFile;
                db.SaveChanges();
                return mtrl;
            }
        }

        public List<Material> PageList(string cas, string cnname, string enname, string keyword,
            string sort, int start, int limit, out int total)
        {
            using (var db = new Abp.Mtrl.MtrlDbcontext())
            {
                var q = db.Material.AsQueryable();
                if (!string.IsNullOrEmpty(cas))
                {
                    q = q.Where(x => x.CASNo.Equals(cas));
                }

                if (!string.IsNullOrEmpty(cnname))
                {
                    q = q.Where(x => x.CnName.Contains(cnname));
                }
                if (!string.IsNullOrEmpty(enname))
                {
                    q = q.Where(x => x.EnName.Contains(enname));
                }
                if (!string.IsNullOrEmpty(keyword))
                {
                    q = q.Where(x => x.CASNo.Equals(keyword) || x.CnName.Contains(keyword) || x.EnName.Contains(keyword));
                }

                //if (!string.IsNullOrEmpty(sort))
                //{
                //    var type = typeof(Mtrl.Material);
                //    var propertyName = sort;
                //    //
                //    var param = Expression.Parameter(type, type.Name);
                //    var body = Expression.Property(param, propertyName);
                //    dynamic keySelector = Expression.Lambda(body, param);

                //    q = q.OrderBy(
                //}
                total = q.Count();
                return q.OrderBy(x=>x.Id).Skip(start).Take(limit).ToList();

            }
        }


        public List<Abp.Mtrl.Material> Query(List<string> cas, List<string> cnname, List<string> enname, List<string> formula)
        {
            using (var db = new Abp.Mtrl.MtrlDbcontext())
            {
                var q = db.Material.AsQueryable();

                if (cas.Count > 0)
                {
                    q = q.Where(x => cas.Any(t=>t.Equals(x.CASNo)));
                } 
                if (cnname.Count > 0)
                {
                    q = q.Where(x => cnname.Contains(x.CnName));
                }
                if (enname.Count > 0)
                {
                    q = q.Where(x => enname.Contains(x.EnName));
                }
                if (formula.Count > 0)
                {
                    q = q.Where(x => formula.Any(t=>t.Equals(x.MolecularFormula)));
                }

                return q.ToList();
            }
        }

        public Abp.Mtrl.Material Save(Abp.Mtrl.Material sub)
        {
            using (var db = new Abp.Mtrl.MtrlDbcontext())
            {
                db.Material.Add(sub);
                db.SaveChanges();
                return sub;
            }
        }

    }
}
